package rs.udd.service;

import java.io.File;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

@Service
public class MailServiceImpl implements MailService {

	@Autowired
	private JavaMailSender mailSender;

	@Autowired
	private SimpleMailMessage simpleMailMessage;

	private static final Logger LOGGER = LogManager.getLogger(MailServiceImpl.class);

	@Override
	public void sendMail(String to, String subject, String body) {
		LOGGER.info("Sending mail...");

		simpleMailMessage.setTo(to);
		simpleMailMessage.setSubject(subject);
		simpleMailMessage.setText(body);
		mailSender.send(simpleMailMessage);
	}

	@Override
	public void sendMail2(String to, String subject, String body) {
		JavaMailSenderImpl sender = new JavaMailSenderImpl();
		sender.setHost("mail.host.com");

		MimeMessage message = sender.createMimeMessage();

		// use the true flag to indicate you need a multipart message
		try {
			MimeMessageHelper helper = new MimeMessageHelper(message, true);
			helper.setTo("test@host.com");

			// use the true flag to indicate the text included is HTML
			helper.setText("<html><body><img src='cid:identifier1234'></body></html>", true);

			// let's include the infamous windows Sample file (this time copied to c:/)
			FileSystemResource res = new FileSystemResource(new File("c:/udd_support_team.png"));
			helper.addInline("identifier1234", res);
			
		} catch (MessagingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		sender.send(message);
		
	}

}
